package com.ve.s2g

import com.natpryce.konfig.ConfigurationProperties
import com.natpryce.konfig.Key
import com.natpryce.konfig.stringType
import com.ve.s2g.chain.AbstractHandler
import com.ve.s2g.chain.HandlerService
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.boot.Banner
import org.springframework.boot.CommandLineRunner
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.context.annotation.Bean

@SpringBootApplication
open class S2GApplication {

    @Bean
    open fun commandLineRunner(handlerService: HandlerService): CommandLineRunner {
        return CommandLineRunner{
            handlerService.finishingHandler()!!.deal(null)
        }
    }

}


fun main(args: Array<String>) {
    runApplication<S2GApplication>(args = *args) {
        setBannerMode(Banner.Mode.OFF)
    }
}

inline fun <reified T : Any> getLogger() = LoggerFactory.getLogger(T::class.java) as Logger

inline fun getS2gConfig(): ConfigurationProperties? {
    if(S2G_CONFIG==null){
        val activeProfiles :String = if (System.getenv("activeProfile")!=null){System.getenv("activeProfile")} else ""
        val symbol = if (activeProfiles.length > 0) "-" else ""
        val s2gFileName ="s2g${symbol}${activeProfiles}.properties"
        S2G_CONFIG = ConfigurationProperties.fromResource(s2gFileName)
    }
    return S2G_CONFIG
}
inline fun getString4Cfg(step:Int, key:String):String{
    return getS2gConfig()!![Key("${AbstractHandler.prefix}.step.${step}.${key}", stringType)]
}
var S2G_CONFIG : ConfigurationProperties? =null